package com.sright.bcard.common.service.share.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTimeUtils {

    public static final String DATA_FORMAT_yyyy_MM_dd = "yyyy-MM-dd";
    public static final String DATA_FORMAT_yyyyMMdd = "yyyyMMdd";
    public static final String DATA_FORMAT_yyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String DATE_TIME_STYLE = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_STYLE_yyyyMMddHHmmSSS = "yyyyMMddHHmmssSSS";
    public static final String DATA_FORMAT_MM_dd_HH_mm = "MM月dd日 HH:mm";
    public static final String DATA_FORMAT_HH_mm = "HH:mm";

    public static final String DAY = "day";
    public static final String HOUR = "hour";
    public static final String MINUTE = "minute";
    public static final String SECOND = "second";
    /*   private static final SimpleDateFormat DATE_TIME_FORMAT         = new SimpleDateFormat(DATE_TIME_STYLE);*/

    /**
     * @param date   格式化前的时间
     * @param format 需要转换的格式
     * @return 格式化后的时间
     * @throws ParseException
     */
    public static Date formatDate(String date, String format) {
        try {
            if (date == null) {
                return null;
            }
          /*  DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
            LocalDateTime dateTime = LocalDateTime.parse(date, df);
            Date date1=Date.from(dateTime.toInstant(ZoneOffset.of("+8")));
            return date1;*/
            SimpleDateFormat df = new SimpleDateFormat(format);
            df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            Date e = new SimpleDateFormat(format).parse(date);
            return e;
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatString(Date date, String format) {
        try {
            if (date == null) {
                return null;
            }
            return new SimpleDateFormat(format).format(date);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 返回当前时间 返回的数据类型是Timestamp
     *
     * @return
     */
    public static Timestamp getNowTime() {
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

    /**
     * 判断某个时间是否在七天内
     *
     * @param addtime
     * @param now
     * @return
     */
    public static boolean isLatestWeek(Date addtime, Date now) {
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(now);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7);  //设置为7天前
        Date before7days = calendar.getTime();   //得到7天前的时间
        if (before7days.getTime() < addtime.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    //获取两个日期的间隔天数
    public static long getDiffDays(Date date1, Date date2) {
        long diff = 0;
        try {
            diff = date2.getTime() - date1.getTime();
            diff = diff / 1000 / 60 / 60 / 24;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }


    /**
     * 时间相加
     *
     * @param date
     * @param num
     * @param timeUnit
     * @return
     */
    public static Date addTime(Date date, int num, String timeUnit) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (DAY.equals(timeUnit)) {
            cal.add(Calendar.DATE, num);
        } else if (HOUR.equals(timeUnit)) {
            cal.add(Calendar.HOUR, num);
        } else if (MINUTE.equals(timeUnit)) {
            cal.add(Calendar.MINUTE, num);
        } else if (SECOND.equals(timeUnit)) {
            cal.add(Calendar.SECOND, num);
        }
        return cal.getTime();

    }

    /**
     * 获取精确到秒的时间戳
     *
     * @param date
     * @return
     */
    public static int getSecondTimestamp(Date date) {
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime() / 1000);
        return Integer.valueOf(timestamp);
    }

    public static String[] getBeforeSevenDay(Integer day) {
        if (day == null) {
            day = 0;
        }
        String[] arr = new String[7];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = null;
        for (int i = 0; i < 7; i++) {
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -(i + day));
            arr[6 - i] = sdf.format(c.getTime());

        }
        return arr;
    }

    public static String getAfterDay(Integer day) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_STYLE);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, +day);

        return sdf.format(c.getTime());
    }

    public static String getBeforeDay(Integer day) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_STYLE);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -day);

        return sdf.format(c.getTime());
    }

    /**
     * long类型转换成string的时间
     *
     * @param commentTime
     * @return
     */
    public static String longTransStringDate(Long commentTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // time为转换格式后的字符串
        return simpleDateFormat.format(new Date(commentTime));
    }

    /**
     * 格式化时间
     *
     * @return
     */
    public static Date formateTimeStart(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String substring = date.substring(0, 11);
        substring += "00:00:00";
        return simpleDateFormat.parse(substring);
    }

    public static Date formateTimeEnd(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String substring = date.substring(0, 11);
        substring += "23:59:59";
        return simpleDateFormat.parse(substring);
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_STYLE);//注意月份是MM
        Date date = simpleDateFormat.parse("2020-06-17 15:32:07");
        Calendar now = Calendar.getInstance();
        now.setTime(formatDate("2020-10-14 14:20:58",DATE_TIME_STYLE));
        now.set(Calendar.DATE, now.get(Calendar.DATE) + 17);
        System.out.println(simpleDateFormat.format(now.getTime()));
    }

    /**
     * 获取距离今天的日期
     *
     * @param date
     * @return
     */
    public static Long days(Date date) {
        Date d = new Date();
        // 输出间隔天数 getTime获取的是毫秒
        return (date.getTime() - d.getTime()) / 24 / 60 / 60 / 1000;
    }

    /**
     * 得到几天后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public Date getDateAfter(Date d, int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);//+后 -前
        return now.getTime();

    }
}
